package com.ls.es.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ls.es.entity.Book;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description:
 * @Author liu Weidong
 * @Email:
 * @Date: 2024/7/6 14:38
 * @Version 1.0
 */
@Service
public class EsServcie {

    @Resource
    private ElasticsearchRestTemplate esTemplate;

    public void createTable() {
        boolean res = esTemplate.createIndex(Book.class);
        esTemplate.putMapping(Book.class);
        System.out.println("创建索引结果：" + res);
        // esTemplate.s
    }

    public void batch() {
        File dirFile = new File("D:\\book\\book");
        File[] dirs = dirFile.listFiles();
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        for (File dir : dirs) {
            List<File> files = FileUtil.loopFiles(dir);
            for (File file : files) {
                threadPool.execute(() -> {
                    List<String> lines = FileUtil.readLines(file, StandardCharsets.UTF_8);
                    ArrayList<IndexQuery> indexQueries = new ArrayList<>();
                    for (String line : lines) {
                        try {
                            JSONObject jsonObject = JSONUtil.toBean(line, JSONObject.class);
                            String author = jsonObject.getStr("author");
                            author = author.replace("著", "").replace("译", "").trim();
                            String press = jsonObject.getStr("press");
                            press = press.trim();
                            String img = jsonObject.getStr("img");
                            String name = jsonObject.getStr("name");
                            String publishTime = jsonObject.getStr("publish_time");
                            String type = jsonObject.getStr("type");
                            Double priceN = jsonObject.getDouble("price_n");
                            Double priceO = jsonObject.getDouble("price_o");
                            String source = dir.getName();
                            String des = jsonObject.getStr("desc");
                            if (des == null) {
                                des = jsonObject.getStr("des");
                            }
                            Book book = new Book();
                            book.setAuthor(author);
                            book.setPress(press);
                            book.setImg(img);
                            book.setName(name);
                            book.setDes(des);
                            book.setSource(source);
                            book.setCurrentPrice(priceN);
                            book.setOriginalPrice(priceO);
                            book.setType(type);
                            book.setPublishTime(publishTime);
                            IndexQuery indexQuery = new IndexQueryBuilder().withObject(book).build();
                            indexQueries.add(indexQuery);
                            if (indexQueries.size() == 1000) {
                                esTemplate.bulkIndex(indexQueries);
                                indexQueries.clear();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        esTemplate.bulkIndex(indexQueries);
                        indexQueries.clear();
                        System.out.println("入库完成================》" + file.getName());

                    }
                });
            }
        }
    }
}
